package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.common.SystemConstant;
import com.app.util.Result;
import com.app.util.UploadUtils;
import com.app.util.WaterMarkUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Controller
@RestController
public class UploadController {

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public Result<Object> upload(HttpServletRequest request,
                                 @RequestParam("file") MultipartFile file) throws IOException {

        if(!file.isEmpty()){
            if(!UploadUtils.imageTypeRight(file))
            {
                return Result.error(500,"文件格式错误");
            }
            //上传文件路径
            String path = "";
            path= SystemConstant.DEFAULT_PDF_URL;
            //获得上传文件名
            String fileName = file.getOriginalFilename();
            int lastIndexOf = fileName.lastIndexOf(".");
            String date = getRandomString2(8);
            //获取文件的后缀名 .jpg
            String suffix = fileName.substring(lastIndexOf);

            File filePath = new File(path + File.separator + date+suffix);
            System.out.println(filePath);
            //如果文件目录不存在，创建目录
            if(!filePath.getParentFile().exists()){
                filePath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件中
            file.transferTo(filePath);
            String imgURL = "";
            //szjy.gshwsc.com  域名
//           imgURL =  SystemConstant.DEFAULT_UPLOAD_SERVICE_URL+date+suffix;
            imgURL =  "/file/"+date+suffix;
            return Result.success(imgURL);


//            // 获取当前日期并格式化
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
//            String datePath = sdf.format(new Date());
//            JSONObject object = new JSONObject();
//            Integer file_Type = 2;
//            if (suffix.equals(".jpg") || suffix.equals(".png") || suffix.equals(".jpeg") || suffix.equals(".gif") || suffix.equals(".bmp")){
//                file_Type = 1;
//            }
//            object.put("url", "/file/" + datePath + "/" + date + suffix);
//            object.put("name", fileName);
//            object.put("size", FileUtils.byteCountToDisplaySize(filePath.length()));
//            object.put("type", file_Type);
//            object.put("filetype", suffix);
//            return Result.success(object);
        }else{
            return Result.error(500,"上传失败");
        }

    }





    /**
     * 上传文件(加水印)
     */
    @PostMapping("/uploadWatermark")
    public Result<String> uploadWatermark(HttpServletRequest request,
                                 @RequestParam("file") MultipartFile file) throws IOException {

        if(!file.isEmpty()){
            //上传文件路径
            String path = "";
            path= SystemConstant.DEFAULT_PDF_URL;
            //获得上传文件名
            String fileName = file.getOriginalFilename();
            int lastIndexOf = fileName.lastIndexOf(".");
            String date = getRandomString2(8);
            //获取文件的后缀名 .jpg
            String suffix = fileName.substring(lastIndexOf);

            File filePath = new File(path + File.separator + date+suffix);
            System.out.println(filePath);
            //如果文件目录不存在，创建目录
            if(!filePath.getParentFile().exists()){
                filePath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件中
            file.transferTo(filePath);
            String imgURL = "";
            //szjy.gshwsc.com  域名
            imgURL =  SystemConstant.DEFAULT_UPLOAD_SERVICE_URL+date+suffix;
                WaterMarkUtils.ImageWatermark(path+"code2.png",path+date+suffix,path+date+suffix,null);
                System.out.println("*****图片上传成功*****");

            return Result.success(imgURL);
        }else{
            return Result.error(500,"上传失败");
        }

    }



    //可以指定字符串的某个位置是什么范围的值
    public static String getRandomString2(int length){
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(3);
            long result=0;
            switch(number){
                case 0:
                    result=Math.round(Math.random()*25+65);
                    sb.append(String.valueOf((char)result));
                    break;
                case 1:
                    result=Math.round(Math.random()*25+97);
                    sb.append(String.valueOf((char)result));
                    break;
                case 2:
                    sb.append(String.valueOf(new Random().nextInt(10)));
                    break;
            }


        }
        return sb.toString();
    }
}
